package com.lbw.chatMemory;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.messages.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class MySqlRAGChatMemory implements ChatMemory {

    @Autowired
    private ChatMemoryRepository mySqlRAGMemoryRepository;

    /**
     * 根据会话id保存会话记录
     * @param conversationId
     * @param messages
     */
    @Override
    public void add(String conversationId, List<Message> messages) {
        log.info("调用add方法");
        log.info("conversationId:{}", conversationId);
        log.info("messages:{}", messages);
        mySqlRAGMemoryRepository.saveAll(conversationId,messages);
    }

    /**
     * 根据会话id获取会话记录
     * @param conversationId
     * @return
     */
    @Override
    public List<Message> get(String conversationId) {
        log.info("调用get方法");
        log.info("conversationId:{}", conversationId);
        List<Message> messageList = mySqlRAGMemoryRepository.findByConversationId(conversationId);
        return messageList;
    }

    /**
     * 根据会话id删除会话记录
     * @param conversationId
     */
    @Override
    public void clear(String conversationId) {
        log.info("调用clear方法，会话id：{}", conversationId);
        mySqlRAGMemoryRepository.deleteByConversationId(conversationId);
    }
}
